﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TSCC.Interface;
using TSCC.Service;
//using Excel = Microsoft.Office.Interop.Excel;

namespace TSCC.WinApp
{
    public partial class frmReport : Form
    {

        protected int colEmpty = 0;
        protected DataTable rptDT = null;
        protected int repeatCol = 0;
        protected string rptName = string.Empty;

        public frmReport()
        {
            InitializeComponent();
        }

        protected void fillExceptionItem()
        {
            List<iCMBItem> itmList = TSCC.Service.Utility.GetExceptionList();
            foreach (iCMBItem itm in itmList)
            {
                lstExceptionItem.Items.Add(itm);
            }          
        }

        protected void fillTeam()
        {
            lstTeam.DataSource = TSCC.Service.Utility.GetTeam();
            lstTeam.SetSelected(0, false);
        }

        private void frmReport_Load(object sender, EventArgs e)
        {
            fillTeam();
            fillExceptionItem();            
            ucDate1.Select();
        }

       
        protected List<string> getSelectItem()
        {
            List<string> item = new List<string>();
            foreach(CMBItem itm in lstExceptionItem.SelectedItems)
            {
                item.Add(Convert.ToString(itm.GetItemValue()));
            }
                      
            return item;
        }

        protected List<string> getSelectTeam()
        {
            List<string> team = new List<string>();
            foreach (string str in lstTeam.SelectedItems)
            {
                team.Add(str);
            }
            
            return team;
        }

        protected string getRptName(string p_name)
        {
            return string.Format("{0}_{1}.xls", DateTime.Now.ToString("yyyy-MM-dd"), p_name);
        }

        private void ucBtn1_Click(object sender, EventArgs e)
        {
            try
            {
                colEmpty = 2;
                repeatCol = 2;
                rptName = getRptName(ucBtn1.Caption);
                Service_Report rpt = new Service_Report();
                bool ok = rpt.EndDateGreatThanStart(ucDate1.TextBoxText, ucDate2.TextBoxText);
                if (!ok)
                {
                    MessageBox.Show("日期錯誤");
                    return;
                }
                List<string> dates = new List<string>();
                dates.Add(ucDate1.TextBoxText);
                dates.Add(ucDate2.TextBoxText);
                List<string> item = getSelectItem();
                List<string> team = getSelectTeam();
                rptDT = rpt.GetReportData(dates, item, team, ReportCategory.Summary1);
                ucGV.SetData(rptDT);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void ucBtn2_Click(object sender, EventArgs e)
        {
            try
            {
                colEmpty = 2;
                repeatCol = 3;
                rptName = getRptName(ucBtn2.Caption);
                Service_Report rpt = new Service_Report();
                bool ok = rpt.EndDateGreatThanStart(ucDate1.TextBoxText, ucDate2.TextBoxText);
                if (!ok)
                {
                    MessageBox.Show("日期錯誤");
                    return;
                }
                List<string> dates = new List<string>();
                dates.Add(ucDate1.TextBoxText);
                dates.Add(ucDate2.TextBoxText);
                List<string> item = getSelectItem();
                List<string> team = getSelectTeam();
                rptDT = rpt.GetReportData(dates, item, team, ReportCategory.Summary2);
                ucGV.SetData(rptDT);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void ucExcel_Click(object sender, EventArgs e)
        {
            if (rptDT == null)
            {
                MessageBox.Show("請先執行查詢再按轉出！");
                return;
            }
            try
            {
                //ucCreateHtmlTable1.XLSPath = @"c:\";
                ucCreateHtmlTable1.XLSPath =
                    string.Format( @"{0}\",System.Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));                     
                ucCreateHtmlTable1.XLSFileName = rptName;
                ucCreateHtmlTable1.CreateHtmlTable(rptDT,colEmpty,repeatCol);
                MessageBox.Show("已轉出！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }

        //private void ucExcel_Click(object sender, EventArgs e)
        //{
        //    if (rptDT == null)
        //    {
        //        MessageBox.Show("請先執行查詢再按轉出！");
        //        return;
        //    }

        //    Excel.Application xlApp = null;
        //    Excel.Workbook xlWorkBook = null;
        //    Excel.Worksheet xlWorkSheet = null;
        //    object misValue = System.Reflection.Missing.Value;

        //    xlApp = new Excel.ApplicationClass();
        //    xlWorkBook = xlApp.Workbooks.Add(misValue);
        //    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

        //    //string xlsName = string.Format(@"C:\{0}.xls",DateTime.Now.ToShortDateString());
        //    string xlsName = string.Format(@"C:\{0}.xls", DateTime.Now.ToString("yyyy-MM-dd"));
            
        //    int i = 0;
        //    int j = 0;

        //    for (int x = 0; x < rptDT.Columns.Count; x++)
        //    {
        //        xlWorkSheet.Cells[i + 1, x + 1] = rptDT.Columns[x].ColumnName;
        //    }

        //    j = 0;

        //    for (i = 0; i <= ucGV.RowCount - 1; i++)
        //    {
        //        for (j = 0; j <= ucGV.ColumnCount - 1; j++)
        //        {
        //            DataGridViewCell cell = ucGV.GetDataGridViewCell(j, i);
        //            xlWorkSheet.Cells[i + 2, j + 1] = cell.Value;
        //        }
        //    }

        //    xlWorkBook.SaveAs(xlsName
        //        , Excel.XlFileFormat.xlWorkbookNormal
        //        , misValue, misValue, misValue, misValue
        //        , Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);

        //    xlWorkBook.Close(true, misValue, misValue);
        //    xlApp.Quit();

        //    releaseObject(xlWorkSheet);
        //    releaseObject(xlWorkBook);
        //    releaseObject(xlApp);
        //    MessageBox.Show("已轉出！");
        //}

        private void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
                MessageBox.Show("Exception Occured while releasing object " + ex.ToString());
            }
            finally
            {
                GC.Collect();
            }
        }

        private void ucBtn3_Click(object sender, EventArgs e)
        {
            try
            {
                colEmpty = 0;
                repeatCol = 0;
                rptName = getRptName(ucBtn3.Caption);
                Service_Report rpt = new Service_Report();
                bool ok = rpt.EndDateGreatThanStart(ucDate1.TextBoxText, ucDate2.TextBoxText);
                if (!ok)
                {
                    MessageBox.Show("日期錯誤");
                    return;
                }
                List<string> dates = new List<string>();
                dates.Add(ucDate1.TextBoxText);
                dates.Add(ucDate2.TextBoxText);
                List<string> item = getSelectItem();
                List<string> team = getSelectTeam();
                rptDT = rpt.GetReportData(dates, item, team, ReportCategory.Detail1);
                ucGV.SetData(rptDT);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void ucBtn4_Click(object sender, EventArgs e)
        {
            try
            {
                colEmpty = 0;
                repeatCol = 0;
                rptName = getRptName(ucBtn4.Caption);
                Service_Report rpt = new Service_Report();
                bool ok = rpt.EndDateGreatThanStart(ucDate1.TextBoxText, ucDate2.TextBoxText);
                if (!ok)
                {
                    MessageBox.Show("日期錯誤");
                    return;
                }
                List<string> dates = new List<string>();
                dates.Add(ucDate1.TextBoxText);
                dates.Add(ucDate2.TextBoxText);
                List<string> item = getSelectItem();
                List<string> team = getSelectTeam();
                rptDT = rpt.GetReportData(dates, item, team, ReportCategory.Detail2);
                ucGV.SetData(rptDT);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


    }
}
